/*
 * SimulatorUtils.cpp
 *
 *  Created on: Sep 26, 2013
 *      Author: gonprieto
 */

#include "SimulatorUtils.h"
#include <sstream>
#include "Logger.h"

static const std::string TOWER_INBOX_NAME = "./fifos/fifo_torre_control";
static const std::string FLIGHT_INBOX_PREFIX = "./fifos/f";
static const std::string FLIGHTCONTROLER_INBOX_PREFIX = "./fifos/fc";
static const std::string RUNWAY_NAME = "./fifos/runway";

std::string SimulatorUtils::getRunwayName() {
	std::stringstream ss;
	ss << RUNWAY_NAME;

	return ss.str();
}

std::string SimulatorUtils::getFlighInboxName(unsigned int flightId) {
	std::stringstream ss;
	ss << FLIGHT_INBOX_PREFIX;
	ss << flightId;

	return ss.str();
}

std::string SimulatorUtils::getTowerInboxName() {
	std::stringstream ss;
	ss << TOWER_INBOX_NAME;
	return ss.str();
}

std::string SimulatorUtils::getFlighControlerInboxName(unsigned int flightId) {
	std::stringstream ss;
	ss << FLIGHTCONTROLER_INBOX_PREFIX;
	ss << flightId;

	return ss.str();
}

int SimulatorUtils::random_in_range(unsigned int max, unsigned int min) {
	int r;
	const unsigned int range = max - min;
	const unsigned int buckets = RAND_MAX / range;
	const unsigned int limit = buckets * range;

	/* Create equal size buckets all in a row, then fire randomly towards
	 * the buckets until you land in one of them. All buckets are equally
	 * likely. If you land off the end of the line of buckets, try again. */
	do {
		r = rand();
	} while (r >= limit);
/*	Logger::instancia() << "SimulatorUtils::random_in_range - random : " << r
			<< " max :" << max << " min:" << min << " buckets:" << buckets
			<< " resultado:" << (int) (min + (r / buckets)) << eol;
*/
	return min + (r / buckets);
}
